"""
正则表达式就是使用字符、转义字符和特殊字符等组成的一个规则
使用这个规则对文本内容进行搜索、匹配和替换等功能
正则表达式的组成：
    普通字符：大小写字符、数字及其组合等
    转义字符：\w,\n,\s
    特殊字符：. * ? + {}
    匹配模式：I U .....
"""
import re

str_01 = "Today is good day 521 it's good weather 521 day"
reg_01 = "521"
reg_02 = "\d+"  # \d 转义字符，代表单个的数字
pattern_01 = re.compile(reg_01)
pattern_02 = re.compile(reg_02)
res_01 = pattern_01.findall(str_01)
res_02 = pattern_02.findall(str_01)
res_03 = pattern_02.sub("l", str_01)
# res_01 = re.findall(reg_01, str_01)
# res_02 = re.findall(reg_02, str_01)
print("res_01:", res_01)
print("res_02:", res_02)
print("res_03:", res_03)
"""
re.match()      从字符串的第一个字符开始匹配
re.search()     从字符串依次开始搜索,找到第一个符合匹配条件的内容
匹配成功则返回一个match对象
group()   获取数据
span()    获取下标区间
匹配失败则返回None


re.findall()    匹配所有符合规则的内容
re.finditer()   匹配所有符合规则的内容
re.sub()        搜索替换指定内容并返回一个新的字符串
re.split()      按照指定规则进行数据切割
re.compile()    按照规则生成一个正则对象
"""
reg_03 = "Today "
res_03 = re.match(reg_03, str_01)
print(res_03)
# 获取字符内容及位置
print(res_03.group())
print(res_03.span(), type(res_03.span()))
reg_04 = "day"
res_04 = re.search(reg_04, str_01)
print(res_04)
res_05 = re.findall(reg_04, str_01)
print(res_05)
res_06 = re.finditer(reg_04, str_01)
print(res_06)
for i in res_06:
    print(i.group())
res_07 = re.sub(reg_04, "Sunday", str_01)
print(str_01)
print(res_07)
res_08 = re.split("521", str_01)
print(res_08, type(res_08))
arr_01 = ["jkdjfks123jklas123 ",
          "jkdjfks456jklas456 ",
          "jkdjfks789jklas789 ",
          "jkdjfks741jklas741 ",
          ]
reg_05 = re.compile("\d+")
for i in arr_01:
    res = reg_05.split(i)
    print(res)
